package com.ooqiu.gaming.ui.main.thread.video;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ooqiu.gaming.R;
import com.ooqiu.gaming.api.entities.Article;
import com.ooqiu.gaming.components.glide.GlideApp;

import java.util.List;

/**
 * @author Eenie
 * @date 2017/11/20
 */

public class VideoContentAdapter extends BaseMultiItemQuickAdapter<Article, BaseViewHolder> {

    public static final int ITEM_SINGLE_PIC_CONTENT = 1;
    public static final int ITEM_MULTI_PIC_CONTENT = 2;
    public static final int ITEM_VIDEO_CONTENT = 3;


    public VideoContentAdapter(@Nullable List<Article> data) {
        super(data);
        addItemType(ITEM_SINGLE_PIC_CONTENT, R.layout.item_single_pic_content);
        addItemType(ITEM_MULTI_PIC_CONTENT, R.layout.item_multi_pic_content);
        addItemType(ITEM_VIDEO_CONTENT, R.layout.item_video_content);
    }

    @Override
    protected void convert(BaseViewHolder helper, Article item) {


        switch (item.getContent_type()) {
            case ITEM_SINGLE_PIC_CONTENT:
                helper.setText(R.id.tv_title, item.getTitle())
                        .setText(R.id.tv_author, item.getWriter())
                        .setText(R.id.tv_cat_name, item.getTypename());


                ImageView imgPic = helper.getView(R.id.img_pic);
                generateImg(imgPic, item.getLitpic());

                break;
            case ITEM_MULTI_PIC_CONTENT:
                helper.setText(R.id.tv_title, item.getTitle())
                        .setText(R.id.tv_author, item.getWriter())
                        .setText(R.id.tv_cat_name, item.getTypename());

                ImageView imgPic1 = helper.getView(R.id.img_pic1);
                ImageView imgPic2 = helper.getView(R.id.img_pic2);
                ImageView imgPic3 = helper.getView(R.id.img_pic3);

                generateImg(imgPic1, item.getLitpic());
                generateImg(imgPic2, item.getLitpic1());
                generateImg(imgPic3, item.getLitpic2());


                break;

            case ITEM_VIDEO_CONTENT:

                helper.setText(R.id.tv_title, item.getTitle())
                        .setText(R.id.tv_author, item.getWriter());
//                        .setText(R.id.tv_cat_name, item.getTypename());

                ImageView mImgHeader = helper.getView(R.id.img_header);

                if (mContext != null) {
                    GlideApp.with(mContext.getApplicationContext())
                            .load(item.getAvatar())
                            .placeholder(R.mipmap.ic_default_header)
                            .error(R.mipmap.ic_default_header)
                            .into(mImgHeader);
                }


                if (!TextUtils.isEmpty(item.getVideotime())) {
                    helper.setText(R.id.tv_duration, item.getVideotime());
                    helper.getView(R.id.tv_duration).setVisibility(View.VISIBLE);
                } else {
                    helper.getView(R.id.tv_duration).setVisibility(View.GONE);
                }


                ImageView imgPic4 = helper.getView(R.id.img_pic);

                if (mContext != null) {
                    GlideApp.with(mContext.getApplicationContext()).load(item.getLitpic1())
                            .error(new ColorDrawable(Color.BLACK))
                            .into(imgPic4);
                }
//                generateImg(imgPic4, item.getLitpic1());


                break;
        }
    }


    private void generateImg(ImageView iv, String imgPath) {


        if (!TextUtils.isEmpty(imgPath)) {
            iv.setVisibility(View.VISIBLE);
            if (mContext != null) {
                GlideApp.with(mContext.getApplicationContext()).load(imgPath)
                        .error(new ColorDrawable(Color.BLACK))
                        .into(iv);
            }
        } else {
            iv.setVisibility(View.GONE);
        }
    }


}
